/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * MonthPlanExporter.java
 * business
 * 2012-12-17上午9:49:32
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.report.data;

import java.io.ByteArrayOutputStream;
import java.util.Collection;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.jackysoft.entity.MonthPlanning;
import org.jackysoft.entity.MonthPlanningItem;

public class MonthPlanExporter implements DataExporter {

	private MonthPlanning plan;
	
	
	
	
	public MonthPlanExporter(MonthPlanning plan) {
		super();
		this.plan = plan;
	}




	@Override
	public byte[] toExcel() throws Exception {
		if(plan==null) return new byte[0];
		Collection<MonthPlanningItem> datas =  plan.getDataItems();
		HSSFWorkbook workbook = new HSSFWorkbook();
		Sheet sheet = workbook.createSheet();
		int rowIndex = 0;
		for(MonthPlanningItem item:datas){
	       Row row = sheet.createRow(rowIndex++);
	       Cell code = row.createCell(0, Cell.CELL_TYPE_STRING);
	       code.setCellValue(item.getOwner().getCode());
	       Cell name = row.createCell(1, Cell.CELL_TYPE_STRING);
	       name.setCellValue(item.getOwner().getName());
	       Cell fval = row.createCell(2, Cell.CELL_TYPE_NUMERIC);
	       fval.setCellValue(item.getFinalValue());
	       Cell tech = row.createCell(3, Cell.CELL_TYPE_STRING);
	       tech.setCellValue(item.getTechnics()==null?"":item.getTechnics().getName());
	       Cell remark = row.createCell(4, Cell.CELL_TYPE_STRING);
	       remark.setCellValue(item.getRemark());
	       
		}
		ByteArrayOutputStream ous = new ByteArrayOutputStream();
		workbook.write(ous);
		return ous.toByteArray();		
	}

}
